﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Fuyao.Business.Entities.Models;
using Fuyao.Business.Logic.BC;
using Fuyao.Win.UI.Util;
using Fuyao.Win.UI.Common;

namespace Fuyao.Win.UI.Ventas
{
    public partial class frmListGuiaRemision : Form
    {
        BusinessLogic bc = new BusinessLogic();
        List<GuiaRemision> listGuiaRemision = new List<GuiaRemision>();

        public frmListGuiaRemision()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.ControlBox = false;
            this.WindowState = FormWindowState.Maximized;
            this.BringToFront();
        }

        private void btnNuevaGuiaRemision_Click(object sender, EventArgs e)
        {
            Ventas.frmGuiaRemision frm = new Ventas.frmGuiaRemision();
            frm.ShowDialog();
            if (frm.action) cargarGrillaGuiaRemision(false);
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMaximizar_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void btnRestaurar_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        private void txtNroGuiaRemision_KeyPress(object sender, KeyPressEventArgs e)
        {
            Utilitarios.vSoloNumeros(e);
            if (e.KeyChar == Convert.ToChar(Keys.Enter)) this.txtCliente.Focus();
        }

        private void txtCliente_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter)) this.cboConductor.Focus();
        }

        private void cboConductor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter)) this.btnBuscarGuiaRemision.Focus();
        }

        private void frmListGuiaRemision_Load(object sender, EventArgs e)
        {
            Utilitarios.configurarGrilla(ref dgvGuiaRemision);
            setColumnasGrilla();
            cargarGrillaGuiaRemision(true);
            Utilitarios.cargarCboConductor(this.cboConductor);
        }

        private void cargarGrillaGuiaRemision(bool _flag)
        {
            GuiaRemision beGuiaRemision = new GuiaRemision();
            beGuiaRemision.NroGuiaRemision = Convert.ToInt32(this.txtNroGuiaRemision.Text.Trim() == string.Empty ? "0" : this.txtNroGuiaRemision.Text.Trim());
            beGuiaRemision.NombreCliente = this.txtCliente.Text.Trim() == string.Empty ? "0" : this.txtCliente.Text.Trim();
            beGuiaRemision.FechaCreacion = Convert.ToDateTime(this.dtpFechaCreacion.Value.ToShortDateString());
            beGuiaRemision.FechaDesde = Convert.ToDateTime(this.dtpFechaCreacionTO.Value.ToShortDateString());
            beGuiaRemision.NombreConductor = this.cboConductor.SelectedIndex == 0 ? "0" : this.cboConductor.Text;
            listGuiaRemision = bc.getAllGuiaRemisionByFilter(beGuiaRemision);
            if (listGuiaRemision.Count > 0) dgvGuiaRemision.DataSource = listGuiaRemision;
            else
            {
                if (!_flag)
                {
                    MessageBox.Show("No se encontraron registros, verifique los filtros de búsqueda", "Sistema FUYAO - Mensaje de Confirmación", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    dgvGuiaRemision.DataSource = null;
                }
            }
        }

        private void setColumnasGrilla()
        {
            ColNroGuiaRemisionDesc.DataPropertyName = "NroGuiaRemisionDesc";
            colCliente.DataPropertyName = "NombreCliente";
            ColDireccion.DataPropertyName = "DireccionCliente";
            colFechaCreacion.DataPropertyName = "FechaCreacion";
            colConductor.DataPropertyName = "NombreConductor";
            colCantidadTotal.DataPropertyName = "Total";            
        }

        private bool validarFiltrosFecha()
        {
            bool isValid = true;
            if (Convert.ToDateTime(dtpFechaCreacion.Value.ToShortDateString()) > Convert.ToDateTime(dtpFechaCreacionTO.Value.ToShortDateString()))
            {
                MessageBox.Show("La Fecha Inicial no puede ser mayor a la Fecha de Fin", "Sistema FUYAO - Validando opción", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                isValid = false;
            }
            return isValid;
        }

        private void btnEditarGuiaRemision_Click(object sender, EventArgs e)
        {
            if (dgvGuiaRemision.RowCount == 0)
            {
                MessageBox.Show("No existen registros para visualizar", "Sistema FUYAO - Validando Opción", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dgvGuiaRemision.CurrentRow != null && dgvGuiaRemision.CurrentRow.DataBoundItem != null)
            {
                GuiaRemision oGuiaRemision = (GuiaRemision)dgvGuiaRemision.CurrentRow.DataBoundItem;
                Ventas.frmGuiaRemision frm = new Ventas.frmGuiaRemision();
                frm.GuiaRemisionID = oGuiaRemision.GuiaRemisionID;
                frm.ShowDialog();
                if (frm.action) cargarGrillaGuiaRemision(false);
            }
            else
            {
                MessageBox.Show("Seleccione un registro para Editar", "Sistema FUYAO - Validando Opción", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }        
        }

        private void btnBuscarGuiaRemision_Click(object sender, EventArgs e)
        {
            if (validarFiltrosFecha()) cargarGrillaGuiaRemision(false);      
        }
    }
}
